<template>
  <div class="userAdd" style="width: 95%">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="医务账号" prop="account">
        <el-input
          v-model="form.account"
          placeholder="请输入账号名/手机号/邮箱"
        ></el-input>
      </el-form-item>
      <el-form-item label="账号权限" prop="purview">
        <el-select v-model="form.purview" placeholder="请选择活动区域">
          <el-option label="医生" value="医生"></el-option>
          <el-option label="护士" value="护士"></el-option>
          <el-option label="实习助理" value="实习助理"></el-option>
          <el-option label="药房" value="药房"></el-option>
          <el-option label="用户" value="用户"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="医务密码" prop="password1">
        <el-input v-model="form.password1" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="password">
        <el-input v-model="form.password" placeholder="请确认密码"></el-input>
      </el-form-item>
      <el-form-item label="医务姓名" prop="name">
        <el-input
          v-model="form.name"
          placeholder="请输入该医务人员姓名"
        ></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio-group v-model="form.sex">
          <el-radio label="男"></el-radio>
          <el-radio label="女"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="form.phone" placeholder="请输入手机号"></el-input>
      </el-form-item>
      <el-form-item label="上传图片" prop="head">
        <el-upload
          class="avatar-uploader"
          action="http://129.211.169.131:21001/users/uploadheader"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="form.head" :src="form.head" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('form')"
          >确认创建</el-button
        >
        <el-button>返回上一级</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { UserAdd } from "../../apis/user";
export default {
  data() {
    return {
      form: {
        account: "",
        password: "",
        name: "",
        sex: "",
        idcard: "",
        purview: "",
        phone: "",
        head: "",
        password1: "",
      },

      //   上传图片
      imageUrl: "",

      // 表单验证
      rules: {
        account: [
          { required: true, message: "请输入账号", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        purview: [{ required: true, message: "请选择权限", trigger: "change" }],

        password1: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],

        password: [
          { required: true, message: "请输入确认密码", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],

        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],

        sex: [{ required: true, message: "请选择性别", trigger: "change" }],

        phone: [{ required: true, message: "请输入手机号", trigger: "blur" }],

        head: [{ required: true, message: "请上传图片", trigger: "change" }],

        // date1: [
        //   { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
        // ],
        // date2: [
        //   { type: 'date', required: true, message: '请选择时间', trigger: 'change' }
        // ],
        // type: [
        //   { type: 'array', required: true, message: '请至少选择一个活动性质', trigger: 'change' }
        // ],
        // resource: [
        //   { required: true, message: '请选择活动资源', trigger: 'change' }
        // ],
        // desc: [
        //   { required: true, message: '请填写活动形式', trigger: 'blur' }
        // ]
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          UserAdd({
            account: this.form.account,
            password: this.form.password,
            name: this.form.name,
            sex: this.form.sex,
            idcard: 370,
            purview: this.form.purview,
            phone: this.form.phone,
            head: this.imageUrl,
          }).then((res) => {
            console.log(res);
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
      // console.log(formName);
      // console.log(this.$refs[formName]);
    },
    // 上传图片
    handleAvatarSuccess(res, file) {
      console.log(res, file, "111111");
      // this.form.head = URL.createObjectURL(file.raw);
      this.form.head = res.header_url;
      this.imageUrl=res.header
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style lang="less" scoped>
.userAdd {
  margin: 10px;
  padding: 20px;
  background-color: #fff;
  border-radius: 10px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
